// pages/customer/information/information.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageType: 0, //0:信息；1：编辑；2：添加
    ctypes: ['个人', '企业'],
    ctypesIndex: '',
    sourcesIndex: '', //来源的选择
    // projectsIndex: '',//项目的选择
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.pageType == 2) {
      var title = '添加'
      this.setData({
        pageType: options.pageType
      })
    } else {
      var title = '信息';
      wx.showLoading({
        title: '加载中...',
        mask: true
      });
      this.getInfo(options.id);
    }
    wx.setNavigationBarTitle({
      title: title,
    })
    this.setData({
      scrollViewHeigth: app.globalData.windowHeight - 124,
    })
    this.getSources();
    // this.getProjects();
  },

  /**
   * 获取客户来源,
   */
  getSources: function() {
    var that = this;
    var data = {
      point_url: app.server.adminApi.getSources,
    };

    app.server.getConnect(data).then(function(res) {
      console.log(res);
      var sources = [];
      for (var i in res.data) {
        sources.push(res.data[i].source_name)
      }
      that.setData({
        sources: sources
      })
    })
  },

  /**
  * 获取项目
  */
  // getProjects: function() {
  //   var that = this;
  //   var data = {
  //     point_url: app.server.adminApi.getProjects,
  //   };

  //   app.server.getConnect(data).then(function(res) {
  //     console.log(res);
  //     var projects = [];
  //     for (var i in res.data) {
  //       projects.push(res.data[i].name)
  //     }
  //     that.setData({
  //       projects: projects
  //     })
  //   })
  // },

  getInfo: function(id) {
    var that = this;
    var data = {
      point_url: app.server.adminApi.getCustomer,
      member_id: id
    };

    app.server.getConnect(data).then(function(res) {
      console.log(res);
      that.setData({
        info: res.data[0],
        ctypesIndex: '',
        sourcesIndex: '',
        // projectsIndex: ''
      });
      wx.hideLoading();
    })
  },

  updateInfo: function(e) {
    var that = this;
    var data = {
      point_url: app.server.adminApi.updateCustomer,
      member_id: this.data.info.id,
      name: e.detail.value.name,
      mobile: e.detail.value.mobile,
      email: e.detail.value.email,
      source: !that.data.sourcesIndex ? that.data.info.source : that.data.sources[that.data.sourcesIndex],
      'type': !that.data.ctypesIndex ? that.data.info.type : that.data.ctypesIndex,

      company: e.detail.value.company,
      remarks: e.detail.value.remarks ? e.detail.value.remarks : that.data.info.remarks,
      // project: that.data.projectsIndex?that.data.projects[that.data.projectsIndex]:that.data.info.project,
      realname: e.detail.value.realname,
      position: e.detail.value.position,
      qq: e.detail.value.qq,
      wechat: e.detail.value.wechat
    };
    console.log(data)

    var msg = '';
    if (!e.detail.value.name) {
      app.showConfirm('提示', '请输入用户名', false);
    } else if (!e.detail.value.mobile) {
      app.showConfirm('提示', '请输入手机号码', false);
    } else if (!e.detail.value.email) {
      app.showConfirm('提示', '请输入邮箱', false)
    } else {
      console.log(data);
      app.server.getConnect(data).then(function(res) {
        console.log(res);
        wx.showToast({
          title: '更新成功',
        })
        that.setData({
          pageType: 0
        });
        wx.setNavigationBarTitle({
          title: '信息'
        })
        that.getInfo(that.data.info.id);
      }, function (res) {
        // app.showConfirm('提示', res.data.msg, false);
      })
    }
  },

  addCustomer: function(e) {
    var that = this;
    var data = {
      point_url: app.server.adminApi.createCustomer,
      admin_id: app.globalData.userInfo.id,
      admin_name: app.globalData.userInfo.name,
      name: e.detail.value.name,
      mobile: e.detail.value.mobile,
      email: e.detail.value.email,
      source: that.data.sources[that.data.sourcesIndex],
      'type': that.data.ctypesIndex,

      company: e.detail.value.company,
      remarks: e.detail.value.remarks,
      // project: that.data.projects[that.data.projectsIndex],
      realname: e.detail.value.realname,
      position: e.detail.value.position,
      qq: e.detail.value.qq,
      wechat: e.detail.value.qq,
      remarks:that.data.content,
      one_log: that.data.one_log
    };
    console.log(data)

    var msg = '';
    if (!e.detail.value.name) {
      app.showConfirm('提示', '请输入客户用户名', false);
    } else if (!that.data.ctypesIndex) {
      app.showConfirm('提示', '请选择客户类型', false)
    } else if (!e.detail.value.mobile) {
      app.showConfirm('提示', '请输入客户手机号码', false);
    } else if (!e.detail.value.email) {
      app.showConfirm('提示', '请输入客户邮箱', false)
    }
    else if (!that.data.sourcesIndex) {
      app.showConfirm('提示', '请选择客户来源', false)
    }
    else {
      app.server.getConnect(data).then(function(res) {
        console.log(res);
        wx.showToast({
          title: '添加成功',
        })
        setTimeout(function() {
          wx.navigateBack({});
        }, 1000);
      }, function(res) {
        app.showConfirm('提示', res.data.msg, false);
      })
    }
  },

  // bindProjectChange: function(e) {
  //   console.log(e.detail.value);
  //   this.setData({
  //     projectsIndex: e.detail.value
  //   })
  // },

  bindCtypeChange: function(e) {
    console.log(e.detail.value);
    this.setData({
      ctypesIndex: e.detail.value
    })
  },

  bindSourceChange: function(e) {
    console.log(e.detail.value);
    this.setData({
      sourcesIndex: e.detail.value
    })
  },

  bindContent: function(e) {
    this.setData({
      content: e.detail.value
    })
  },

  bindLogContent: function(e) {
    this.setData({
      one_log: e.detail.value
    })
  },

  bindEdit: function() {
    this.setData({
      pageType: 1
    });
    wx.setNavigationBarTitle({
      title: '编辑'
    })
  },

  bindCancel: function() {
    this.setData({
      pageType: 0
    });
    wx.setNavigationBarTitle({
      title: '信息'
    })
  },

  bindSubmit: function() {
    wx.setNavigationBarTitle({
      title: '信息'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
